jsontodom.js ➔ jsonToDOM   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 56
rs 8.44
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A jsontodom.js ➔ ... ➔ namespace 0 4 1
B jsontodom.js ➔ ... ➔ tag 0 46 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
jsonToDOM.namespaces = {
2
	html: "http://www.w3.org/1999/xhtml",
3
	xul: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
4
};
5
6
jsonToDOM.defaultNamespace = jsonToDOM.namespaces.html;
7
8
function jsonToDOM(jsonTemplate, doc, nodes) {
9
	function namespace(name) {
10
		var reElemNameParts = /^(?:(.*):)?(.*)$/.exec(name);
11
		return { namespace: jsonToDOM.namespaces[reElemNameParts[1]], shortName: reElemNameParts[2] };
12
	}
13
14
	// Note that 'elemNameOrArray' is: either the full element name (eg. [html:]div) or an array of elements in JSON notation
15
	function tag(elemNameOrArray, elemAttr) {
16
		// Array of elements?  Parse each one...
17
		if (Array.isArray(elemNameOrArray)) {
18
			var frag = doc.createDocumentFragment();
19
			Array.prototype.forEach.call(arguments, function(thisElem) {
20
				frag.appendChild(tag.apply(null, thisElem));
21
			});
22
			return frag;
23
		}
24
25
		// Single element? Parse element namespace prefix (if none exists, default to defaultNamespace), and create element
26
		var elemNs = namespace(elemNameOrArray);
27
		var elem = doc.createElementNS(elemNs.namespace || jsonToDOM.defaultNamespace, elemNs.shortName);
28
29
		// Set element's attributes and/or callback functions (eg. onclick)
30
		for (var key in elemAttr) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
31
			var val = elemAttr[key];
32
			if (nodes && key == "key") {
33
				nodes[val] = elem;
34
				continue;
35
			}
36
37
			var attrNs = namespace(key);
38
			if (typeof val == "function") {
39
				// Special case for function attributes; don't just add them as 'on...' attributes, but as events, using addEventListener
40
				elem.addEventListener(key.replace(/^on/, ""), val, false);
41
			}
42
			else {
43
				// Note that the default namespace for XML attributes is, and should be, blank (ie. they're not in any namespace)
44
				elem.setAttributeNS(attrNs.namespace || "", attrNs.shortName, val);
45
			}
46
		}
47
48
		// Create and append this element's children
49
		var childElems = Array.prototype.slice.call(arguments, 2);
50
		childElems.forEach(function(childElem) {
51
			if (childElem != null) {
52
				elem.appendChild(
53
					childElem instanceof doc.defaultView.Node ? childElem :
54
						Array.isArray(childElem) ? tag.apply(null, childElem) :
55
						doc.createTextNode(childElem));
56
			}
57
		});
58
59
		return elem;
60
	}
61
62
	return tag.apply(null, jsonTemplate);
63
}
64